mini.parse();

function GoodsSupplierHandler() {
};

GoodsSupplierHandler.instance = new GoodsSupplierHandler();

GoodsSupplierHandler.isQueryBtnExpended = false;

//获得全局的供货商数据表格对象
GoodsSupplierHandler.goodsSupplierGrid = mini.get("goodsSupplierGrid");

//加载表格数据
GoodsSupplierHandler.goodsSupplierGrid.load();

//供货商状态数据
GoodsSupplierHandler.brandStateData = [
	{text: '全部', id: -1},
	{text: '无效', id: 0},
	{text: '有效', id: 1}
];

mini.get("query_bsiState").setData(GoodsSupplierHandler.brandStateData);

/**
 * 根据用户权限展示功能按钮
 */
GoodsSupplierHandler.loadToolbar = function() {
	var array = new Array("supplier_list_add", "supplier_list_edit","supplier_list_del","supplier_list_query","supplier_list_refresh");
	var userArr = MiniuiTools.isHasPurview(array);
	for ( var i = 0; i < userArr.length; i++) {
		mini.get(array[i]).setVisible(userArr[i]);
	}
};
GoodsSupplierHandler.loadToolbar();

/**
 * 弹出添加供货商窗口
 */
GoodsSupplierHandler.addGoodsSupplier = function() {
	miniWin.afreshNewWin();
	miniWin.setParams(374, 394, "添加供货商");
	miniWin.setOption("bodyStyle","padding:0px;");

	miniWin.load("goodsSupplierForm.jsp",function(){
		var iframe = miniWin.getIFrameEl();
        var data = {action: "new"};
        iframe.contentWindow.GoodsSupplierFormHandler.passData(data);
	},function(action){
		GoodsSupplierHandler.goodsSupplierGrid.reload();
	});
	
	miniWin.show();
};

/**
 * 弹出编辑供货商窗口
 */
GoodsSupplierHandler.editGoodsSupplier = function() {
	var rowData = GoodsSupplierHandler.goodsSupplierGrid.getSelected();
	var rowsData = GoodsSupplierHandler.goodsSupplierGrid.getSelecteds();
	if(rowsData.length == 1){
		miniWin.afreshNewWin();
		miniWin.setParams(374, 394, "编辑供货商[<font color='blue'>"	+ rowData.bsiName + "</font>]");
		miniWin.setOption("bodyStyle","padding:0px;");
		
		miniWin.load("goodsSupplierForm.jsp",function(){
			var iframe = miniWin.getIFrameEl();
            var data = {action: "edit", bsiId: rowData.bsiId};
            iframe.contentWindow.GoodsSupplierFormHandler.passData(data);
		},function(action){
			GoodsSupplierHandler.goodsSupplierGrid.reload();
		});
		
		miniWin.show();
	}else if(rowsData.length > 1){
		MiniuiTools.miniWarning("系统提示","不能同时编辑多条资源！");
	}else{
		MiniuiTools.miniWarning("系统提示","请选择要编辑的资源！");
	}
	
};

/**
 * 删除供货商资源
 */
GoodsSupplierHandler.delGoodsSupplier = function() {
	var rowsData = GoodsSupplierHandler.goodsSupplierGrid.getSelecteds();
	if(rowsData.length > 0){
		mini.confirm("确认删除选择的" + rowsData.length + "条记录？" ,"系统提示",
			function(action, value){
				if (action == "ok") {
					var ids = "";
					for ( var i = 0; i < rowsData.length - 1; i++){
						ids += rowsData[i].bsiId+ ",";
					}
					ids += rowsData[rowsData.length - 1].bsiId;
					
					// 判断选择的供货商记录中是否已被使用
					if(GoodsSupplierHandler.getBgiByBsi(ids)){
						MiniuiTools.miniWarning("系统提示", "选择的记录存在商品信息关联，请检查后重试");
						return;	
					}
					
					GoodsSupplierHandler.goodsSupplierGrid.loading();
                    $.ajax({
                        url: __root + "/delGoodsSupplierByIds.do",
                        data: {
				        	bsiIds: ids,
				        	lslUser: gbOperateUser,
				    		lslAddress: gbOperateIp
				        },
                        success: function (text) {
                        	MiniuiTools.miniInfo("系统提示","操作成功！", function(){
				        		GoodsSupplierHandler.goodsSupplierGrid.reload();
				        	});
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
            				MiniuiTools.miniError("系统提示","操作失败！" + jqXHR.responseText);
                        },
                        complete: function(e){
                        	GoodsSupplierHandler.goodsSupplierGrid.unmask();
                        }
                    });	
                } 
			}
		)
	}else{
		MiniuiTools.miniWarning("系统提示","请选择要删除的资源！");
	}
};

/**
 * 检验选择的记录是否已被商品使用
 * @param {} bsiIds
 * @return {}
 */
GoodsSupplierHandler.getBgiByBsi = function(bsiIds){
	var isUsed = false;
	
	$.ajax({
        url: __root + "/getBgiByBsi.do?bsiIds=" +bsiIds,
        dataType: 'json',
        async: false,   // 同步执行，默认为true
        success: function (text) {
        	if(text == "1"){
        		isUsed = true;
        	}
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
        }
    });
    
    return isUsed;
}

/**
 * 更新供货商状态
 */
GoodsSupplierHandler.updateGoodsSupplierState = function() {
	if (!MiniuiTools.hasPurview("supplier_list_edit")) {
		return;
	}
	var rowData = GoodsSupplierHandler.goodsSupplierGrid.getSelected();
	var bsiId = rowData.bsiId;	
	GoodsSupplierHandler.goodsSupplierGrid.loading();
	
    $.ajax({
        url: __root + "/updateGoodsSupplierStateById.do",
        data: {
        	bsiId: bsiId,
        	lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
        },
        success: function (text) {
            GoodsSupplierHandler.goodsSupplierGrid.reload();
        },
        error: function (jqXHR, textStatus, errorThrown) {
            MiniuiTools.miniError("系统提示","操作失败！" + jqXHR.responseText);
        },
        complete: function(e){
        	GoodsSupplierHandler.goodsSupplierGrid.unmask();
        }
    });	
};

/**
 * 展开或折叠查询供货商面板
 */
GoodsSupplierHandler.queryGoodsSupplier = function() {
	if (GoodsSupplierHandler.isQueryBtnExpended) {
		GoodsSupplierHandler.isQueryBtnExpended = false;
		mini.get("supplier_list_query").setText("展开查询");
		mini.get("supplier_list_query").setIconCls("icon-btn-expand");
	} else {
		GoodsSupplierHandler.isQueryBtnExpended = true;
		mini.get("supplier_list_query").setText("折叠查询");
		mini.get("supplier_list_query").setIconCls("icon-btn-collapse");
	}
	
	$("#queryGoodsSupplierDiv").toggle(500);
    setTimeout(function(){mini.get("goodsSupplierPanel").doLayout();},600);
};

/**
 * 执行查询供货商的方法
 */
GoodsSupplierHandler.doQueryGoodsSupplier = function(){
	var bsiName = mini.get("query_bsiName").getValue();
	var bsiState = mini.get("query_bsiState").getValue();
	
    GoodsSupplierHandler.goodsSupplierGrid.load({bsiName: bsiName, bsiState: bsiState});
};

/**
 * 刷新页面数据及布局
 */
GoodsSupplierHandler.afresh = function(){
	GoodsSupplierHandler.doQueryGoodsSupplier();
	mini.get("goodsSupplierPanel").doLayout();
}

/**
 * DrawCell 绘制单元格
 */
GoodsSupplierHandler.goodsSupplierGrid.on("drawcell", function(e) {
	 var record = e.record;
     var column = e.column;
     var field = e.field;
     var value = e.value;

     if (column.field == "bsiDesc") {
     	if(e.value.length > 50){
     		e.cellHtml = e.value.substring(0,50) + "...";
     	}
     }
     
     if (column.field == "bsiState") {
        if (e.value == 1) {
            e.cellHtml = "<span class='icon-show-yes' style='cursor: pointer;' onclick='GoodsSupplierHandler.updateGoodsSupplierState()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";                    
        } else {
            e.cellHtml = "<span class='icon-show-no' style='cursor: pointer;' onclick='GoodsSupplierHandler.updateGoodsSupplierState()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
        }
    }
});

